package classes;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class BaseServlet extends HttpServlet {

    /*public boolean checkSession(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession(true);
        if(session.isNew()||session.getAttribute("")==null){
            session.invalidate();
            return true;
        }
        return false;
    }*/

    public void redirect(String url, HttpServletRequest request, HttpServletResponse response)
        throws IOException{
        response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+url));
    }

    public String safeURL(String url, HttpServletRequest request, HttpServletResponse response)
        throws IOException{
        return response.encodeURL(request.getContextPath()+url);
    }

    public void dispatch (String url, HttpServletRequest request, HttpServletResponse response)
    {
        try {
            RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
            rd.forward(request, response);
        } catch (ServletException ex) {
            Logger.getLogger(BaseServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(BaseServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    /** Returns a short description of the servlet.*/
    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
